package scalab.utils

trait Reporter{
	val name: String

	private[scalab] def fail(msg: Any): Unit = {
		error(msg)
		System.exit(1)
	}

	def error(msg: Any): Boolean = notify(Reporter.ERROR_LEVEL, msg)
	def warning(msg: Any): Boolean = notify(Reporter.WARNING_LEVEL, msg)
	//def note(msg: Any): Unit = Report.note(name, msg)
	def info(msg: Any): Boolean = notify(Reporter.INFO_LEVEL, msg)
	def debug(msg: Any): Boolean = notify(Reporter.DEBUG_LEVEL, msg)
	/*private[scalab]*/def succes(msg: Any): Boolean = notify(Reporter.SUCCES_LEVEL, msg)

	private var messageLevel = Reporter.defaultMessageLevel
	final def setMessageLevel(level: Int){
		messageLevel = level
	}
	final def getMessageLevel: Int = messageLevel

	final def notify(level: Int, msg: Any): Boolean = {
		if(Reporter.checkMessageLevel(level) && level >= messageLevel){
			Console.println(header(level) + msg)
			true
		}
		else
			false
	}

	final def header(level: Int): String = 
		"[" + Reporter.getColor(level) + name + Console.RESET + "] "

	final def flush = Console.flush
}

object Reporter extends Reporter{

	val name = "Scalab"

	//default color values
	val ERROR_COLOR = Console.BOLD + Console.RED
	val SUCCES_COLOR = Console.BLUE
	val INFO_COLOR = Console.WHITE
	val WARNING_COLOR = Console.YELLOW
	val DEBUG_COLOR = Console.WHITE

	private var colors: Array[String] = Array(DEBUG_COLOR, INFO_COLOR, WARNING_COLOR, ERROR_COLOR, SUCCES_COLOR)
	def setColor(level: Int, color: String): Boolean = {
		if(checkMessageLevel(level)){
			colors.update(level, color)
			true
		}
		else
			false
	}
	def getColor(level: Int): String = 
		if(checkMessageLevel(level))colors(level)
		else null

	val DEBUG_LEVEL = 0
	val INFO_LEVEL = 1
	val WARNING_LEVEL = 2
	val ERROR_LEVEL = 3
	val SUCCES_LEVEL = 4
	val NOTHING_LEVEL = 5

	final def checkMessageLevel(level: Int): Boolean = 
		level >= 0 && level < 5

	//val PROGRAM_NAME = "Scalab"

	private var defaultMessageLevel = INFO_LEVEL// | Reporter.ERROR_LEVEL
	final def setDefaultMessageLevel(level: Int){
		defaultMessageLevel = level
	}
	final def getDefaultMessageLevel: Int = defaultMessageLevel

}
